package com.jiezuo.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezuo.auth.entity.Depart;
import com.jiezuo.auth.entity.Permission;
import com.jiezuo.auth.entity.Role;
import com.jiezuo.auth.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface UserMapper extends BaseMapper<User> {

    //通过用户名查询用户信息
    @Select("select * from sys_user where user_name=#{user_name}")
    User getUserByUserName(String user_name);

    //通过用户名或姓名查询用户信息
    @Select("select * from sys_user where user_name=#{user_name} OR real_name=#{user_name}")
    User getUserByUserNameOrRealName(String user_name);


    //通过用户ID查询用户信息
    User getUserByUserId(String id);

    //通过用户ID查询用户所有权限资源信息
    List<Permission> getPermissionByUserId(@Param("user_id") String user_id,
                                           @Param("resource_type") String resource_type,
                                           @Param("is_mobile") String is_mobile,
                                           @Param("belongs") String belongs
    );

    List<User> findList(@Param("page") Page page, @Param("entity") User user);

    List<User> findList(@Param("entity") User user);

    //通过用户查询部门
    @Select("SELECT sd.* FROM sys_depart_user sdu " +
            "LEFT JOIN sys_depart sd ON sd.id=sdu.depart_id AND sd.is_delete=0 " +
            "WHERE sdu.user_id=#{user_id} AND sd.id IS NOT NULL ")
    List<Depart> getDepartsByUserId(@Param("user_id") String user_id);

    //通过用户查询角色
    @Select("SELECT sr.* FROM sys_user_role sur " +
            "LEFT JOIN sys_role sr ON sr.id=sur.role_id AND sr.is_delete=0 " +
            "WHERE sur.user_id=#{user_id} AND sr.id IS NOT NULL ")
    List<Role> getRolesByUserId(@Param("user_id") String user_id);

    List<User> findBusinessList();

    List<User> findJiuzhouByRoleId();

    List<Map<String, Object>> findSignList();

    @Select("SELECT " +
            "  se.id AS main_enterprise, " +
            "  sd.id AS main_dept  " +
            "FROM sys_user su " +
            "LEFT JOIN sys_depart sd ON su.main_dept = sd.id " +
            "LEFT JOIN sys_enterprise se ON sd.affiliation_enterprise = se.id " +
            "WHERE su.id=#{user_id} AND sd.is_delete=0 AND se.is_delete=0 " +
            "AND se.ancestors IS NOT NULL AND sd.ancestors IS NOT NULL")
    List<Map<String, String>> findAncestorsByUserId(@Param("user_id") String user_id);

    @Select("SELECT sp.id AS group_ids FROM sys_user_role sur " +
            "LEFT JOIN sys_role_permission srp ON sur.role_id=srp.role_id " +
            "LEFT JOIN sys_permission sp ON sp.id=srp.permission_id " +
            "WHERE sp.link_type=1 AND sur.user_id=#{user_id} " +
            "GROUP BY sp.id")
    List<String> findGroupIdsByUserId(@Param("user_id") String user_id);

    @Select(" SELECT * FROM flw_my_group fmg WHERE FIND_IN_SET(#{user_id},fmg.groups) and fmg.is_delete=0 ")
    List<String> findFlowGroupIdsByUserId(@Param("user_id") String user_id);

    List<User> findDirectorById(@Param("id") String id);

    List<User> findUserInfoByDepartId(@Param("depart_id") String depart_id);

    List<User> findUserInfoByUser(@Param("page") Page page, @Param("entity") User user);

    List<User> getUserByDepartsOrderByDepartIdAndUserId(@Param("departs") List<String> departs);

//    List<ExportUser> findAllUser(@Param("entity") ExportUser exportUser);
//
//    List<ExportUser> findAllUserMenu(@Param("entity") ExportUser exportUser);
//
//    List<ExportUser> findAllUserRole(@Param("entity") ExportUser exportUser);

    @Select("SELECT sr.id AS role_id,sr.role_name,sr.data_authority_level,sr.role_depart,sp.id,sp.resource_name,sp.parent_id,sp.link_type FROM sys_user_role sur " +
            "LEFT JOIN sys_role sr ON sr.id=sur.role_id AND sr.is_delete=0 " +
            "LEFT JOIN sys_role_permission srp ON srp.role_id=sur.role_id " +
            "LEFT JOIN sys_permission sp ON srp.permission_id=sp.id AND sp.is_delete=0 " +
            "WHERE sur.user_id=#{userId} ORDER BY sr.id ASC ")
    List<Map<String, Object>> getRoleDetailsByUserId(@Param("userId") String userId);


//    @Select("SELECT " +
//            " DISTINCT sd.ancestors " +
//            "FROM " +
//            " sys_depart sd " +
//            " LEFT JOIN sys_depart_user sdu ON sd.id = sdu.depart_id  " +
//            "WHERE  " +
//            "  sd.is_delete = 0  " +
//            " AND sd.`status` = 1  " +
//            " AND sdu.user_id = #{user_id} ")
//    List<String> findAllAncestorByUserId(@Param("user_id") String user_id);
//
//    @Select({"<script>",
//            "SELECT su.* FROM sys_depart_user sdu ",
//            " LEFT JOIN sys_user su ON su.id=sdu.user_id AND su.is_delete=0 AND su.status=0 ",
//            " WHERE 1=1 AND sdu.depart_id IN(SELECT sd.id FROM sys_depart sd WHERE sd.is_delete=0 AND sd.is_branch = 1 AND sd.id in ",
//            "   <foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>",
//            "        #{id} ",
//            "   </foreach>",
//            ") ",
//            "</script>"})
//    List<User> findUsersByDepartId(@Param("ids") List<String> ids);

    @Select("SELECT " +
            " FIND_IN_SET(195,sd.ancestors) AS a, " +
            " FIND_IN_SET(196,sd.ancestors) AS b, " +
            " FIND_IN_SET(197,sd.ancestors) AS c, " +
            " FIND_IN_SET(198,sd.ancestors) AS d " +
            "FROM sys_user su " +
            "LEFT JOIN sys_depart sd ON sd.id=su.main_dept AND sd.is_delete=0 " +
            "WHERE su.id=#{user_id} ")
    List<Map<String, Long>> isDepartByUserId(@Param("user_id") String user_id);

    @Select({"<script>",
            "   SELECT su.* FROM sys_user su  ",
            "        LEFT JOIN sys_depart sd ON sd.id=su.main_dept AND sd.is_delete=0  ",
            "        WHERE su.is_delete=0  ",
            "        <choose>  ",
            "            <when test=\"depart_id !=null and depart_id !=''\">  ",
            "                AND FIND_IN_SET(#{depart_id},sd.ancestors)  ",
            "            </when>  ",
            "            <otherwise>  ",
            "                AND (  ",
            "                    FIND_IN_SET(195,sd.ancestors) OR  ",
            "                    FIND_IN_SET(196,sd.ancestors) OR  ",
            "                    FIND_IN_SET(197,sd.ancestors) OR  ",
            "                    FIND_IN_SET(198,sd.ancestors)  ",
            "                )  ",
            "            </otherwise>  ",
            "        </choose>",
            "        <if test=\"real_name !=null and real_name !=''\"> ",
            "               AND su.real_name LIKE concat('%',#{real_name},'%') ",
            "        </if>",
            "</script>"})
    List<User> findUsersByDepartId(@Param("page") Page page, @Param("depart_id") String depart_id, @Param("real_name") String real_name);

    //通过用户查询角色
    @Select("SELECT su.* FROM sys_user su LEFT JOIN sys_user_role sur ON su.id=sur.user_id LEFT JOIN sys_role sr ON sur.role_id= sr.id WHERE su.main_dept=#{depart_id} AND sr.code='ROLE_YJYYZ'")
    List<User> getChargeList(@Param("depart_id") String depart_id);

    Map<String,Object> findGroupList(@Param("name") String name);

    @Select(" SELECT sr.* FROM sys_user_role sur " +
            " LEFT JOIN sys_role sr ON sr.id=sur.role_id " +
            " WHERE sur.user_id=#{user_id} AND sr.is_delete=0 ")
    List<Role> findRolesByUserId(@Param("user_id") String user_id);

    @Select("select * from sys_user su where su.is_delete = 0 and find_in_set(#{positionId}, su.positions) and su.main_dept = #{departId}")
    List<User> getDirectorByDepartId(@Param("departId")String departId, @Param("positionId")String positionId);

    @Select("select * from sys_user su where su.is_delete = 0 and su.real_name = #{entity.real_name}")
    List<User> findUserByRealName(@Param("entity") User user);
}
